/*
 * Copyright (c) 1998, 2023 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0,
 * or the Eclipse Distribution License v. 1.0 which is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
 */

// Contributors:
//     Oracle - initial API and implementation from Oracle TopLink
package org.eclipse.persistence.testing.oxm.mappings.binarydatacollection;

import jakarta.activation.DataHandler;
import org.eclipse.persistence.oxm.attachment.XMLAttachmentUnmarshaller;

import java.io.IOException;
import java.util.HashMap;

public class MyAttachmentUnmarshaller implements XMLAttachmentUnmarshaller {
    public static final String PHOTO_PATH1 = "http://www.example.com/admin/images/ocom/oralogo_small.gif";
    public static final String PHOTO_BASE64 = "/9j/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAAUCAATAJoEASIAAhEBAxEBBCIA/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiM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2sfRt/MvzCiiiuc0/TrTW/hGL3UoRcXVksi28zEhkAbgZHUc9DmvJ6KK8nEpcsH5H6Hk0pOriY30U3ZdvQKKKK6v4bW0N14706OeNZEG9wrdMhCQfzFUfGkaxeNdYVBgG6dvxJyf1NFFS1/s69f0NYyf9ryV9PZr/ANKYUUUVhV6n8F7O3muNZuJYVeWOOKNWbnCtv3DHvtH5UUU8Gr1o/wBdCOI21llVry/9KQUUUVz3xK0iw0fxL5On2y28ToHKITjJx0B6fQVxtFFRiElVkkdOTzlPAUpTd211CiiivoT4ZWkFv4FsZIYlR5y8krDq7byMn8ABXX0UV7dD+FH0R+XZm28bWb/ml+bCiiiv/9k=";
    public static final String XOP_NAMESPACE_PREFIX = "xop";
    public static final String XOP_NAMESPACE_URL = "http://www.w3.org/2004/08/xop/include";
    public static final String ATTACHMENT_PREFIX = "c_id";
    public static final String ATTACHMENT_TEST_ID = "c_id0";
    public boolean getAttachmentAsDataHandlerWasCalled;

    public HashMap attachments;


    public MyAttachmentUnmarshaller() {
        getAttachmentAsDataHandlerWasCalled = false;
        attachments = new HashMap();
    }

    @Override
    public byte[] getAttachmentAsByteArray(String cid) {
        Object obj = attachments.get(cid);
        if(obj instanceof byte[]){
            return (byte[])obj;
        }

        try {
            return ((String)((DataHandler)obj).getContent()).getBytes();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public DataHandler getAttachmentAsDataHandler(String cid) {
        this.getAttachmentAsDataHandlerWasCalled = true;
        Object obj = attachments.get(cid);
        if (obj instanceof DataHandler) {
            return (DataHandler)obj;
        }
        return null;
    }

    @Override
    public boolean isXOPPackage() {
        // force attachment usage
        return true;
    }

    public boolean getAttachmentAsDataHandlerWasCalled() {
        return this.getAttachmentAsDataHandlerWasCalled;
    }
}
